
-- 查看錢包
local httparg    = require("shared.api.httparg")
local response   = require("shared.api.response")
local config     = require("config")
local tag        = httparg.tag()
local assertion  = httparg.assertion
local httpclient = require "shared.http.client"
local util       = require "proxy.game.common.util"

local ngx        = _G.ngx
local get_response_spec     = util.get_response_spec
local fill_response_header  = util.fill_response_header
local is_match_content_type = util.is_match_content_type
local get_content_decoder   = util.get_content_decoder
local get_method            = util.get_method




local cjson = require "cjson.safe"
      cjson.encode_max_depth(32)
      cjson.decode_max_depth(32)

local ACCESS_TOKEN_TTL = config.VAR.ACCESS_TOKEN_TTL



local ARGS = {
  -- exist body?
  tag.body('json', 'required'),

  -- required 会自动判断是否有填，未填自动返回错误
  username  = tag.json.username  ('string', 'required'),
}




--调用dataGateway实际登录
local resonseReal=function()
  local headers=ngx.req.get_headers()


  --ngx.log(ngx.ERR,clientIP)

  --根ulr
  local ADDRESS = config.ENV.WALLET_SERVICE
  --后面接的path
  local ACTION  = string.format("%s/wallet",ngx.var.oauth_brand)



  if not ADDRESS or not ACTION then
    ngx.status = ngx.HTTP_NOT_FOUND
    ngx.log(ngx.ERR,"not found")
    ngx.exit(ngx.OK)
  end

  local location = ngx.unescape_uri(ADDRESS..ACTION)
  local method   = "LIST"
  local headers  = ngx.req.get_headers()
  ngx.req.read_body()
  --local data   = ngx.req.get_body_data()
  --local data   = cjson.encode(apiReq)
  local callReq={}
  callReq.player=ARGS.username
  callReq.alliance="muse"
  local data=cjson.encode(callReq)
  --ngx.log(ngx.ERR,d)

  local query  = ngx.var.args

  do
    -- add X-Forwarded-For header
    headers['X-Forwarded-For'] = ngx.var.proxy_add_x_forwarded_for
  end





  --移除方法设定
  local rmvLowKey="x-http-method-override"
  --移除gzip设定
  local rmvKey2="accept-encoding"
  for k,v in pairs(headers) do
     local low=string.lower(k)
     if low==rmvLowKey  or low==rmvKey2   then
       headers[k]=nil
     end
  end


  --重新装填 方法；移除或者指定为特定需要的method
  --ngx.log(ngx.ERR,method)
  headers['X-HTTP-Method-Override']=method
  --headers['x-http-method-override']=nil
  
  --[[
  local p=method
  for k,v in pairs(headers) do
      p=p.." k "..k..";v "..v ..";\r\n"
  end
  ngx.log(ngx.ERR,p)
  --]]

 

  -- perform http request
  local resp, err = httpclient.new()
    :uri(location)
    :headers(headers)
    :query(query)
    :body(data)
    :send(method, 2000, nil)






  if err or (not resp) then
    ngx.status = 500
    ngx.say(err)
    ngx.log(ngx.ERR,err)
    ngx.exit(ngx.OK)
  end

  -- response
  ngx.header['Server'] = ngx.var.SERVICE_NAME
  ngx.header['Content-Type'] = resp.headers['Content-Type']
  ngx.status = resp.status

  local rmvLowKey="content-length"
  --如果我们修改了回应的内容，不是和调用的service返回的长度一样，"content-length"需要拿掉，不然回复出去的长度对不上
  for k,v in pairs(resp.headers) do
     local low=string.lower(k)
     if low==rmvLowKey then
      resp.headers[k]=nil
     end
  end

  -- fill header except 'Server'
  fill_response_header(resp.headers)



  local replyInfo=resp.body
  local regRes=cjson.decode(replyInfo)
  --ngx.log(ngx.ERR,type(regRes.wallets )..type({}))
  if  regRes and regRes.wallets and type(regRes.wallets)==type({}) then
    --成功了
    local walletInfo={}
    walletInfo.wallets={}
    for k,v in pairs(regRes.wallets) do
        local one={
          currency=v.currency,
          status=v.status,
          balance=v.balance,
        }
        --ngx.log(ngx.ERR,"insert one")
        table.insert(walletInfo.wallets,one)
    end
    walletInfo.timestamp=ngx.time()
    replyInfo=cjson.encode(walletInfo)
  else
      --失败了，打印信息
      if resp.body then
        ngx.log(ngx.ERR,"dataService resonpce err"..resp.body)
      else
          ngx.log(ngx.ERR,"dataService resonpce err")
      end
  end
  
 
  --ngx.log(ngx.ERR,"rep0---: "..replyInfo)
  ngx.say(replyInfo)
  ngx.exit(ngx.OK)
end



--返回mock数据
local resonseMock=function()
    -- response
    --mock数据
    local RESULT={}
    math.randomseed(os.time())
    RESULT.wallets={}

    local one={
      currency="BTC",
      status="activated",
          balance= "160"
    }
    table.insert(RESULT.wallets,one)

    local one={
      currency="ETH",
      status="activated",
          balance= "170"
    }
    table.insert(RESULT.wallets,one)
    RESULT.timestamp=ngx.time()
    response.success(cjson.encode(RESULT))
end

--返回mock数据
--resonseMock()
--真实数据登录
resonseReal()
